/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.model.expression;

import java.util.List;

import dex.compiler.model.base.Place;
import dex.compiler.model.type.ArrayTypeNode;
import dex.compiler.model.type.TypeNode;


/**
 * A new expression, like <code>new Foo()</code>.
 */
public class NewExpression extends Expression {
	

	/**
	 * The type of thing being constructed.
	 */
	private TypeNode type;
	
	
	/**
	 * The parameters for the construction.
	 */
	private List<? extends Expression> parameters;

	
	/**
	 * Constructs a new expression.
	 * 
	 * @param place  the place where the expression originated
	 * @param type   the type of thing being constructed
	 * @param parameters   the parameters for construction
	 */
	public NewExpression(Place place, TypeNode type, List<? extends Expression> parameters) {
		super(place);
		if (type == null) {
			throw new IllegalArgumentException("type may not be null");
		}
		if (parameters == null) {
			throw new IllegalArgumentException("parameters may not be null");
		}
		this.type = type;
		this.parameters = parameters;
	}
	
	
	/**
	 * Returns the parameters for construction.
	 * 
	 * <p>For class types, these parameters will be passed to one
	 * of the class's constructors, assuming such a constructor exists.
	 * 
	 * <p>For record types, these parameters will be assigned to the
	 * new record's fields in order.
	 * 
	 * <p>For array types, these parameters determine the array dimensions.
	 * 
	 * @return  the list of parameters
	 */
	public List<? extends Expression> getParameters() {
		return parameters;
	}
	
	
	/**
	 * Returns the type of thing being constructed.
	 * May be a class type, a record type or an array type.
	 * 
	 * @return  the type of thing being constructed
	 */
	public TypeNode getType() {
		return type;
	}
	

	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		result.append("new ").append(getBaseType(type));
		if (type instanceof ArrayTypeNode) {
			result.append('[').append(listToString(parameters)).append(']');
		} else {
			result.append('(');
			if (parameters.size() > 0) {
				result.append(parameters.get(0));
				for (int i = 1; i < parameters.size(); i++) {
					result.append(", ").append(parameters.get(i));
				}
			}
			result.append(')');
		}
		return result.toString();
	}
	
	
	private static TypeNode getBaseType(TypeNode t) {
		if (t instanceof ArrayTypeNode) {
			return getBaseType(((ArrayTypeNode)t).getElementType());
		}
		return t;
	}


}
